/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.servlets;

import com.jmooc.entity.Course;
import com.jmooc.entity.CourseModule;
import com.jmooc.service.CourseModuleService;
import com.jmooc.service.CourseService;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Group
 */
@WebServlet(name = "CourseServlet", urlPatterns = {"/course/*"} )
public class CourseServlet extends HttpServlet {

    @EJB
    private CourseService courseService;
    
    @EJB
    private CourseModuleService courseModuleService;
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        Long courseId = Long.valueOf(request.getPathInfo().substring(1));
        
        Course course = null;
        List<CourseModule> courseModules = null;
        
        try {
            course = courseService.getById(courseId);
            courseModules = courseModuleService.getAllByCourse(course);
           
            request.setAttribute("course", course);
            request.setAttribute("courseModules", courseModules);
        } catch (Exception e) {
            request.setAttribute("message", "Ce cours n'existe pas ou plus");
        }
        
        request.getRequestDispatcher("/jsp/course.jsp").forward(request, response);
        System.out.println(course);
    }

}
